package com.kang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kang.bean.Users;
import com.kang.bean.blog.Article;
import com.kang.commons.Result;
import com.kang.commons.util.MapToObjUtil;
import com.kang.mapper.ArticleMapper;
import com.kang.service.ArticleService;
import com.kang.service.userService.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Pageable;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @program: service
 * @description:
 * @author: K.faWu
 * @create: 2022-04-20 16:45
 **/
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private UsersService usersService;

    @Override
    public void saveArticle(Article article, HttpServletRequest request) {
        Result result = usersService.getUsers2(request.getHeader("token"));

        Users users = null;
        try {
            users = MapToObjUtil.map2Obj((Map<String, Object>) result.getData(), Users.class);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            Result.error("系统错误！\n 请联系管理员！");
        }
        if (users== null){
            Result.error("未登录！");
        }
        article.setAuthor(users.getId());
        article.setVersion(new Date());
        articleMapper.insert(article);
    }

    @Override
    public PageInfo<Article> queryByPage(Article article, Pageable page) {
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        PageHelper.startPage(page.getPageNumber(), page.getPageSize());
        List<Article> articles = articleMapper.selectList(queryWrapper);
        return null;
    }
}
